package android.slc.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * BigDecimal公共方法
 *
 * @author slc
 * 2021-1-16 16:53:23
 */
public class SlcBigDecimalUtils {

    /**
     * 大于
     *
     * @param b1 操作数1
     * @param b2 操作数2
     * @return 结果
     */
    public static boolean greaterThan(BigDecimal b1, BigDecimal b2) {
        b1 = ensureBigDecimal(b1);
        b2 = ensureBigDecimal(b2);
        return b1.compareTo(b2) > 0;
    }

    /**
     * 大于或等于
     *
     * @param b1 操作数1
     * @param b2 操作数2
     * @return 结果
     */
    public static boolean greaterOrEquals(BigDecimal b1, BigDecimal b2) {
        b1 = ensureBigDecimal(b1);
        b2 = ensureBigDecimal(b2);
        return b1.compareTo(b2) >= 0;
    }

    /**
     * 小于
     *
     * @param b1 操作数1
     * @param b2 操作数2
     * @return 结果
     */
    public static boolean lessThan(BigDecimal b1, BigDecimal b2) {
        b1 = ensureBigDecimal(b1);
        b2 = ensureBigDecimal(b2);
        return b1.compareTo(b2) < 0;
    }

    /**
     * 小于或等于
     *
     * @param b1 操作数1
     * @param b2 操作数2
     * @return 结果
     */
    public static boolean lessOrEquals(BigDecimal b1, BigDecimal b2) {
        b1 = ensureBigDecimal(b1);
        b2 = ensureBigDecimal(b2);
        return b1.compareTo(b2) <= 0;
    }

    /**
     * 等于
     *
     * @param b1 操作数1
     * @param b2 操作数2
     * @return 结果
     */
    public static boolean equals(BigDecimal b1, BigDecimal b2) {
        b1 = ensureBigDecimal(b1);
        b2 = ensureBigDecimal(b2);
        return b1.compareTo(b2) == 0;
    }

    public static BigDecimal ensureBigDecimal(BigDecimal value) {
        return ensureBigDecimal(value, BigDecimal.ZERO);
    }

    public static BigDecimal ensureBigDecimal(BigDecimal value, Number defValue) {
        if (value == null) {
            if (defValue instanceof BigDecimal) {
                return (BigDecimal) defValue;
            }
            return BigDecimal.valueOf(defValue.doubleValue());
        }
        return value;
    }
    //********************  格式化  *******************************//

    public static class SetScale {
        public static SetScale DEF_SET_SCALE = new SetScale();
        public static final int DEF_SCALE = 2;

        private int scale = DEF_SCALE;

        public void setScale(int scale) {
            this.scale = scale;
        }

        public int getScale() {
            return scale;
        }

        public BigDecimal preSetScale(Object obj) {
            return preSetScale(obj, scale);
        }

        public BigDecimal preSetScale(Object obj, boolean mustScale) {
            return preSetScale(obj, scale, mustScale);
        }

        public BigDecimal preSetScale(Object obj, int scale) {
            return preSetScale(obj, scale, false);
        }

        public BigDecimal preSetScale(Object obj, int scale, boolean mustScale) {
            if (obj instanceof Number) {
                BigDecimal decimal = BigDecimal.valueOf(((Number) obj).doubleValue());
                if (decimal.scale() > scale || mustScale) {
                    decimal = decimal.setScale(scale, RoundingMode.HALF_DOWN);
                }
                return decimal;
            }
            return BigDecimal.valueOf(0.00);
        }

    }

    public static BigDecimal preSetScale(Object obj) {
        return SetScale.DEF_SET_SCALE.preSetScale(obj);
    }

    public static BigDecimal preSetScale(Object obj, boolean mustScale) {
        return SetScale.DEF_SET_SCALE.preSetScale(obj, mustScale);
    }

    public static BigDecimal preSetScale(Object obj, int scale) {
        return SetScale.DEF_SET_SCALE.preSetScale(obj, scale);
    }

    public static BigDecimal preSetScale(Object obj, int scale, boolean mustScale) {
        return SetScale.DEF_SET_SCALE.preSetScale(obj, scale, mustScale);
    }
}
